﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Web;
using System.Xml;
using Com.StellmanGreene.CSVReader;

namespace ChartInfrastructure
{
    public class SettingsReader
    {
        public IList<LineData> GetLines(string filePath)
        {
            var dataTable = CSVReader.ReadCSVFile(filePath, true);

            var lineDatas = new List<LineData>();

            foreach (DataColumn column in dataTable.Columns)
            {
                var lineData = new LineData
                   {
                       Title = column.ColumnName,
                       Points = new double[dataTable.Rows.Count]
                   };

                int i = 0;
                foreach (DataRow row in dataTable.Rows)
                {
                    lineData.Points[i++] = double.Parse(ConvertToString(column, row), CultureInfo.InvariantCulture);
                }

                lineDatas.Add(lineData);
            }


            return lineDatas;
        }

        private static string ConvertToString(DataColumn column, DataRow row)
        {
            var str = row[column.ColumnName].ToString();

            return str == " " ? "0" : str;
        }

        public ChartsSettings GetSettings(string folderName)
        {
            var path = Path.Combine(ChartsConfiguration.ChartPath, folderName);
            var fileName = Path.Combine(path, "charts.xml");

            var doc = new XmlDocument();

            doc.Load(fileName);

            return Read(path, doc);
        }

        private static ChartsSettings Read(string path, XmlDocument doc)
        {
            var timeLineNode = doc.SelectSingleNode("ChartSettings/TimeLine");

            var timelineFile = timeLineNode.Attributes["File"].Value;


            var dataTable = CSVReader.ReadCSVFile(Path.Combine(path, timelineFile), true);
            var firstDate = DateTime.Parse(dataTable.Rows[0][0].ToString(), CultureInfo.InvariantCulture);
            var secondDate = DateTime.Parse(dataTable.Rows[1][0].ToString(), CultureInfo.InvariantCulture);


            var s = new ChartsSettings
                        {
                            StartDateTime = firstDate,
                            Charts = new List<ChartSettings>(),
                            PointInterval = secondDate - firstDate
                        };


            var nodes = doc.SelectNodes("ChartSettings/Charts/Chart");

            foreach (XmlNode node in nodes)
            {
                s.Charts.Add(new ChartSettings
                                 {
                                     File = node.Attributes["File"].Value,
                                     Title = node.Attributes["Title"].Value,
                                     YAxisName = node.Attributes["YAxisName"].Value,
                                     FilePath = Path.Combine(path, node.Attributes["File"].Value)
                                 });
            }

            return s;
        }

        public IList<ReportLinkView> GetDirectories()
        {
            var directories = Directory.GetDirectories(ChartsConfiguration.ChartPath);

            directories = directories.Select(d => d.Substring(d.LastIndexOf("\\") + 1)).ToArray();

            return directories.Select(directory => new ReportLinkView()
                       {
                           Name = directory,
                           Url = "~/Report.aspx?path=" + HttpContext.Current.Server.UrlEncode(directory)
                       }).ToList();
        }
    }
}
